// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    stastics: [],
    array: ['连续缩量', '连跌'],
    optionIndex: 0,
    seachStocks: [],
    riseData: {
      data: [],
      pageNo: 1,
      pageSize: 20
    },
    dropData: {
      data: [],
      pageNo: 1,
      pageSize: 20
    },
    enlargeVolume: {
      data: [],
      pageNo: 1,
      pageSize: 20
    },
    reduceVolume: {
      data: [],
      pageNo: 1,
      pageSize: 20
    },
    optionStastics: {
      data: [],
      pageNo: 1,
      pageSize: 20
    },
    stocks: {
      data: [],
      pageNo: 1,
      pageSize: 30
    },
    orderStastic: app.globleData.orderStastic
  },

  onLoad(options) {
    this.getStastics(1);
    this.getStastics(2);
    this.getStastics(3);
    this.getStastics(4);
    this.getStocks()
  },

  searchStock(e) {
    var keyword = e.detail.value;
    if(keyword == "") {
      return
    }
    wx.request({
      url: app.globleData.url + '/api/data/stock_day_info/seach',
      method: "GET",
      data: {
        keyword: keyword,
        num: 10
      },
      header: {
        cookie: 'auth=' + app.globleData.token
      },
      success:(res) => {
        var tem = res.data.data.slice(0,10)
        this.setData({
          seachStocks: [...tem]
        })
      }
    })
  },

  getStastics(type) {
    var param = this.getPageParam(type)
    wx.request({
      url: app.globleData.url + '/api/analysis/statistics/list',
      method: "POST",
      header: {
        cookie: 'auth=' + app.globleData.token
      },
      data: param,
      success:(res) => {
        this.setStasticData(type, res.data.data)
      }
    })
  },

  setStasticData(type, data) {
    if (1 == type) {
      this.setData({
        riseData: {
          data: data,
          pageNo: this.data.riseData.pageNo + 1,
          pageSize: this.data.riseData.pageSize
        },
      })
    }
    if (2 == type) {
      this.setData({
        dropData: {
          data: data,
          pageNo: this.data.dropData.pageNo + 1,
          pageSize: this.data.dropData.pageSize
        },
      })
      this.setOptionStastics()
    }
    if (3 == type) {
      this.setData({
        enlargeVolume: {
          data: data,
          pageNo: this.data.enlargeVolume.pageNo + 1,
          pageSize: this.data.enlargeVolume.pageSize
        },
      })
    }
    if (4 == type) {
      this.setData({
        reduceVolume: {
          data: data,
          pageNo: this.data.reduceVolume.pageNo + 1,
          pageSize: this.data.reduceVolume.pageSize
        },
      })
    }
    
  },

  getStocks() {
    wx.request({
      url: app.globleData.url + '/api/data/stock_day_info/list',
      method: "POST",
      header: {
        'Content-Type': '	application/json',
        'cookie': 'auth=' + app.globleData.token
      },
      data: {
        startTime: '',
        endTime: '',
        pageNo: this.data.stocks.pageNo,
        pageSize: this.data.stocks.pageSize
      },
      success:(res) => {
        this.setData({
          stocks: {
            data: [...this.data.stocks.data, ...res.data.data.data],
            pageNo: this.data.stocks.pageNo + 1,
            pageSize: this.data.stocks.pageSize
          }
        })
        this.setOptionStastics()
      }
    })
  },

  sortStastics(stastics, field) {
    const target = JSON.parse(JSON.stringify(stastics))
    target.sort((pre, next) => {
      let preValue = pre[field]
      let nextValue = next[field]
      if (!preValue && preValue !== 0) {
        preValue = type === 'descending' ? -(Math.pow(2,53) - 1) : (Math.pow(2,53) - 1)
      }
      if (!nextValue && nextValue !== 0) {
        nextValue = type === 'descending' ? -(Math.pow(2,53) - 1) : (Math.pow(2,53) - 1)
      }
      return preValue - nextValue
    })
    return target
  },

  bindPickerChange(e) {
    console.log(e.detail.value)
    this.setData({
      optionIndex: e.detail.value
    })

    this.setOptionStastics()
  },

  getPageParam(type) {
    if (1 == type) {
      return this.generateStasticParam(type, this.data.riseData.pageNo, this.data.riseData.pageSize)
    }
    if (2 == type) {
      return this.generateStasticParam(type, this.data.dropData.pageNo, this.data.dropData.pageSize)
    }
    if (3 == type) {
      return this.generateStasticParam(type, this.data.enlargeVolume.pageNo, this.data.enlargeVolume.pageSize)
    }
    if (4 == type) {
      return this.generateStasticParam(type, this.data.reduceVolume.pageNo, this.data.reduceVolume.pageSize)
    }
  },

  generateStasticParam(type, pageNo, pageSize) {
    return {
      orderType: type,
      pageNo: pageNo,
      pageSize: pageSize
    }
  },

  setOptionStastics() {
    if (this.data.optionIndex == 0) {
      this.setData({
        optionStastics: this.data.reduceVolume.data
      })
    } else {
      this.setData({
        optionStastics: this.data.dropData.data
      })
    }
  }
})
